package cn.itsource.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static cn.itsource.hrm.mq.Mq.EXCHANGE_NAME;
import static cn.itsource.hrm.mq.Mq.QUEUE_NAME;

//定义一个配置类，主要作用定义交换机和队列，并且建个队列和交换机进行绑定
@Configuration
public class RabbitmqConfig {


    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     *
     * @return the exchange
     */
    @Bean(EXCHANGE_NAME)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        //durable(true)持久化，消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //声明队列
    @Bean(QUEUE_NAME)
    public Queue QUEUE_INFORM_SMS() {
        Queue queue = new Queue(QUEUE_NAME);
        return queue;
    }


    /**
     * channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
     * 绑定队列到交换机 .
     *
     * @param queue    the queue
     * @param exchange the exchange
     * @return the binding
     * 下面在绑定交换机的时候，可以使用调用上面的方法
     */
    @Value("${site.rootingkey}")
   private String rootingKey;
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_NAME) Queue queue,
                                            @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(QUEUE_INFORM_SMS()).to(EXCHANGE_TOPICS_INFORM()).with(rootingKey).noargs();
    }
}
